import java.net.*;
import java.io.*;

public class CoordFinder
{
	/*public static void main(String[] args)
	{
		// TESTING
		// Remove main method when done
		// Change methods to non-static when done
		System.out.println(generateURL(args[0]));
		System.out.println(getAddress(args[0]));
		System.out.println(getLatitude(args[0]));
		System.out.println(getLongitude(args[0]));
	}*/
	public static URL generateURL(String location)
	{
		// Example URL:
		// http://maps.googleapis.com/maps/api/geocode/json?address=Honolulu,+Hawaii&sensor=false
		String temp = location;
		String[] splitLocation = temp.split("\\s+");
		String concatLocation = "http://maps.googleapis.com/maps/api/geocode/json?address=";
		// Appending location tokens into the URL
		for (int i = 0; i < splitLocation.length; i++)
		{
			concatLocation += splitLocation[i];
			if (!(i == splitLocation.length-1))
			{
				concatLocation += "+";
			}
		}
		// Appending end of URL
		concatLocation += "&sensor=false";

		// Create and return URL
		try
		{
			URL locationURL = new URL(concatLocation);
			return locationURL;
		}
		catch(Exception e)
		{
			return null;
		}
	}

	public static String getAddress(String location)
	{
		try
		{
			URL locationURL = generateURL(location);
			BufferedReader in = new BufferedReader(new InputStreamReader(locationURL.openStream()));

			String inputLine;
			String addressLine = "";
			boolean found = false;

			// Search for formatted address line
			while (!found)
			{
				inputLine = in.readLine();
				if (inputLine.contains("formatted_address"))
				{
					addressLine = inputLine;
					found = true;
				}
			}

			// Trim line to just the address
			addressLine = addressLine.substring(32, addressLine.length());
			addressLine = addressLine.substring(0, addressLine.length()-2);

			return addressLine;
		}
		catch(Exception e)
		{
			return null;
		}
	}

	public static Double getLatitude(String location)
	{
		try
		{
			URL locationURL = generateURL(location);
			BufferedReader in = new BufferedReader(new InputStreamReader(locationURL.openStream()));

			String inputLine;
			String latLine = "";
			boolean found = false;

			while (!found)
			{
				inputLine = in.readLine();
				if (inputLine.contains("location"))
				{
					found = true;
				}
			}
			latLine = in.readLine();

			latLine = latLine.substring(23, latLine.length()-2);

			return Double.parseDouble(latLine);
		}
		catch(Exception e)
		{
			return null;
		}
	}

	public static Double getLongitude(String location)
	{
		try
		{
			URL locationURL = generateURL(location);
			BufferedReader in = new BufferedReader(new InputStreamReader(locationURL.openStream()));

			String inputLine;
			String longLine = "";
			boolean found = false;

			while (!found)
			{
				inputLine = in.readLine();
				if (inputLine.contains("location"))
				{
					found = true;
				}
			}
			in.readLine();
			longLine = in.readLine();

			longLine = longLine.substring(23, longLine.length()-2);

			return Double.parseDouble(longLine);
		}
		catch(Exception e)
		{
			return null;
		}
	}
}